Getting started with MovingPandas¶

Binder IPYNB HTML

MovingPandas provides a trajectory datatype based on GeoPandas. The project home is at https://github.com/movingpandas/movingpandas

The documentation is available at https://movingpandas.readthedocs.io/

In [1]:
import pandas as pd
import geopandas as gpd
import movingpandas as mpd
import shapely as shp
import hvplot.pandas 

from geopandas import GeoDataFrame, read_file
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
from holoviews import opts

import warnings
warnings.filterwarnings('ignore')

opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))

mpd.show_versions()
MovingPandas 0.15.rc1

SYSTEM INFO
-----------
python     : 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:39:05) [MSC v.1929 64 bit (AMD64)]
executable : H:\miniconda3\envs\mpd-ex\python.exe
machine    : Windows-10-10.0.19045-SP0

GEOS, GDAL, PROJ INFO
---------------------
GEOS       : None
GEOS lib   : None
GDAL       : 3.5.0
GDAL data dir: None
PROJ       : 9.0.0
PROJ data dir: H:\miniconda3\pkgs\proj-9.0.0-h1cfcee9_1\Library\share\proj

PYTHON DEPENDENCIES
-------------------
geopandas  : 0.12.2
pandas     : 1.5.3
fiona      : 1.8.21
numpy      : 1.24.1
shapely    : 1.8.2
rtree      : 1.0.0
pyproj     : 3.3.1
matplotlib : 3.6.3
mapclassify: None
geopy      : 2.3.0
holoviews  : 1.14.9
hvplot     : 0.8.2
geoviews   : 1.9.6
stonesoup  : 0.1b11

Creating a trajectory from scratch¶

Trajectory objects consist of a trajectory ID and a GeoPandas GeoDataFrame with a DatetimeIndex. The data frame therefore represents the trajectory data as a Pandas time series with associated point locations (and optional further attributes).

Let's create a small toy trajectory to see how this works:

In [2]:
df = pd.DataFrame([
  {'geometry':Point(0,0), 't':datetime(2018,1,1,12,0,0)},
  {'geometry':Point(6,0), 't':datetime(2018,1,1,12,6,0)},
  {'geometry':Point(6,6), 't':datetime(2018,1,1,12,10,0)},
  {'geometry':Point(9,9), 't':datetime(2018,1,1,12,15,0)}
]).set_index('t')
gdf = GeoDataFrame(df, crs=31256)
toy_traj = mpd.Trajectory(gdf, 1)
toy_traj
Out[2]:
Trajectory 1 (2018-01-01 12:00:00 to 2018-01-01 12:15:00) | Size: 4 | Length: 16.2m
Bounds: (0.0, 0.0, 9.0, 9.0)
LINESTRING (0 0, 6 0, 6 6, 9 9)
In [3]:
toy_traj.plot()
Out[3]:
<AxesSubplot: >

We can also access the trajectory's GeoDataFrame:

In [4]:
toy_traj.df
Out[4]:
geometry
t
2018-01-01 12:00:00 POINT (0.000 0.000)
2018-01-01 12:06:00 POINT (6.000 0.000)
2018-01-01 12:10:00 POINT (6.000 6.000)
2018-01-01 12:15:00 POINT (9.000 9.000)
In [5]:
toy_traj.df.plot()
Out[5]:
<AxesSubplot: >

Loading trajectories from CSV¶

In [6]:
df = pd.read_csv('../data/geolife_small.csv', delimiter=';')
df
Out[6]:
X Y fid id sequence trajectory_id tracker t
0 116.391305 39.898573 1 1 1 1 19 2008-12-11 04:42:14+00
1 116.391317 39.898617 2 2 2 1 19 2008-12-11 04:42:16+00
2 116.390928 39.898613 3 3 3 1 19 2008-12-11 04:43:26+00
3 116.390833 39.898635 4 4 4 1 19 2008-12-11 04:43:32+00
4 116.389410 39.898723 5 5 5 1 19 2008-12-11 04:43:47+00
... ... ... ... ... ... ... ... ...
5903 116.337194 39.926232 6993 6993 867 5 2 2009-02-25 14:31:04+00
5904 116.337207 39.926237 6994 6994 868 5 2 2009-02-25 14:31:09+00
5905 116.337259 39.926207 6995 6995 869 5 2 2009-02-25 14:31:14+00
5906 116.337290 39.926201 6996 6996 870 5 2 2009-02-25 14:31:19+00
5907 116.337332 39.926186 6997 6997 871 5 2 2009-02-25 14:31:24+00

5908 rows × 8 columns

In [7]:
traj_collection = mpd.TrajectoryCollection(df, 'trajectory_id', t='t', x='X', y='Y')
print(traj_collection)
TrajectoryCollection with 5 trajectories
In [8]:
traj_collection.plot(column='trajectory_id', legend=True, figsize=(9,5))
Out[8]:
<AxesSubplot: >

Loading trajectories from a GeoPackage¶

In [9]:
gdf = read_file('../data/geolife_small.gpkg')
gdf
Out[9]:
id sequence trajectory_id tracker t geometry
0 1 1 1 19 2008-12-11 04:42:14+00 POINT (116.39131 39.89857)
1 2 2 1 19 2008-12-11 04:42:16+00 POINT (116.39132 39.89862)
2 3 3 1 19 2008-12-11 04:43:26+00 POINT (116.39093 39.89861)
3 4 4 1 19 2008-12-11 04:43:32+00 POINT (116.39083 39.89863)
4 5 5 1 19 2008-12-11 04:43:47+00 POINT (116.38941 39.89872)
... ... ... ... ... ... ...
5903 6993 867 5 2 2009-02-25 14:31:04+00 POINT (116.33719 39.92623)
5904 6994 868 5 2 2009-02-25 14:31:09+00 POINT (116.33721 39.92624)
5905 6995 869 5 2 2009-02-25 14:31:14+00 POINT (116.33726 39.92621)
5906 6996 870 5 2 2009-02-25 14:31:19+00 POINT (116.33729 39.92620)
5907 6997 871 5 2 2009-02-25 14:31:24+00 POINT (116.33733 39.92619)

5908 rows × 6 columns

After reading the trajectory point data from file, we want to construct the trajectories.

In [10]:
traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')
print(traj_collection)
TrajectoryCollection with 5 trajectories
In [11]:
traj_collection.plot(column='trajectory_id', legend=True, figsize=(9,5))
Out[11]:
<AxesSubplot: >
In [12]:
traj_collection.plot()
Out[12]:
<AxesSubplot: >

Accessing individual trajectories¶

In [13]:
my_traj = traj_collection.trajectories[1]
print(my_traj)
Trajectory 2 (2009-06-29 07:02:25 to 2009-06-29 11:13:12) | Size: 897 | Length: 38764.6m
Bounds: (116.319212, 39.971703, 116.592616, 40.082514)
LINESTRING (116.590957 40.071961, 116.590905 40.072007, 116.590879 40.072027, 116.590915 40.072004, 
In [14]:
my_traj.plot(linewidth=5, capstyle='round', figsize=(9,3))
Out[14]:
<AxesSubplot: >

To visualize trajectories in their geographical context, we can also create interactive plots with basemaps:

In [15]:
my_traj.hvplot(line_width=7.0, tiles='OSM')
Out[15]:

Creating a TrajectoryCollection from scratch¶

In [16]:
df = pd.DataFrame(
    {
        "t": pd.date_range("2020-01-01", periods=5, freq="min"),
        "trajectory_id": [1, 1, 2, 2, 2],
        "geometry": [Point(0, 0), Point(0, 1), Point(0, 2), Point(0, 3), Point(0, 4)],
    }
)
gdf = gpd.GeoDataFrame(df, crs=31256)
trajc = mpd.TrajectoryCollection(gdf, traj_id_col='trajectory_id', t='t')
trajc
Out[16]:
TrajectoryCollection with 2 trajectories
In [17]:
min_duration = timedelta(minutes=1)
trajc.trajectories = [traj for traj in trajc if traj.get_duration() > min_duration]
trajc
Out[17]:
TrajectoryCollection with 1 trajectories
In [ ]: